from FinancialAsset.AssetFactory import AssetFactory
from Misc.QuoteType import QuoteType

class Portfolio:
	
	def __init__(self, name, start_time, capital, capital_currency):
		self.portf_name = name
		cash = AssetFactory().acquire_asset('CASH', capital_currency)
		self.portf_activity_list = [(start_time, cash, capital)]
	
	def name(self):
		return self.portf_name
		
	def activity_list(self):
		return self.portf_activity_list
		
	def add_activity(self, activity): # activity = (ref_time, asset, chg_quantity)
		self.portf_activity_list.append(activity)
		
	def holding(self, ref_time):
		holding = {} #{asset:quantity}
		
		for activity in self.portf_activity_list:
		
			if activity[0] <= ref_time:
				
				if holding.has_key(activity[1]):
					holding[activity[1]] += activity[2]
				else:
					holding[activity[1]] = activity[2]
		
		return dict((k, v) for k, v in holding.iteritems() if v != 0) #exclude quantity = 0
			
			
				
	def nav(self, ref_time, quote_type = QuoteType.CLOSE, currency = None):
	
		holding = self.holding(ref_time)
		
		r = 0
		for asset, quantity in holding.iteritems():
			r += asset.npv(ref_time, quote_type, currency) * quantity
		
		return r
